package com.weatherapp.simple;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.weatherapp.simple.adapter.CityAdapter;
import com.weatherapp.simple.model.City;
import java.util.ArrayList;
import java.util.List;

public class CityManagementActivity extends AppCompatActivity implements CityAdapter.OnCityClickListener {

    private ImageView ivBack;
    private ImageView ivAdd;
    private RecyclerView rvCities;
    private CityAdapter cityAdapter;
    private List<City> cityList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_management);
        
        initViews();
        setupRecyclerView();
        loadMockData();
        setupClickListeners();
    }

    private void initViews() {
        ivBack = findViewById(R.id.iv_back);
        ivAdd = findViewById(R.id.iv_add);
        rvCities = findViewById(R.id.rv_cities);
    }

    private void setupRecyclerView() {
        cityList = new ArrayList<>();
        cityAdapter = new CityAdapter(cityList);
        cityAdapter.setOnCityClickListener(this);
        
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvCities.setLayoutManager(layoutManager);
        rvCities.setAdapter(cityAdapter);
    }

    private void loadMockData() {
        cityList.add(new City("北京", 25, "☀️", "晴"));
        cityList.add(new City("上海", 22, "⛅", "多云"));
        cityList.add(new City("广州", 28, "🌧️", "小雨"));
        cityList.add(new City("深圳", 27, "🌥️", "阴"));
        cityList.add(new City("杭州", 24, "☀️", "晴"));
        
        cityAdapter.updateData(cityList);
    }

    private void setupClickListeners() {
        ivBack.setOnClickListener(v -> finish());
        
        ivAdd.setOnClickListener(v -> {
            // 这里可以打开添加城市的对话框或新界面
            showAddCityDialog();
        });
    }

    private void showAddCityDialog() {
        // 简单的添加城市对话框示例
        String[] cities = {"成都", "西安", "南京", "武汉", "重庆", "天津"};
        
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("选择城市")
                .setItems(cities, (dialog, which) -> {
                    String selectedCity = cities[which];
                    // 模拟添加城市（实际应用中需要获取真实天气数据）
                    City newCity = new City(selectedCity, 23, "☀️", "晴");
                    cityList.add(newCity);
                    cityAdapter.notifyItemInserted(cityList.size() - 1);
                    Toast.makeText(this, "已添加 " + selectedCity, Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    @Override
    public void onCityClick(City city) {
        // 点击城市，切换到该城市的天气
        Intent resultIntent = new Intent();
        resultIntent.putExtra("selected_city", city.getName());
        setResult(RESULT_OK, resultIntent);
        finish();
    }

    @Override
    public void onDeleteCity(City city, int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("删除城市")
                .setMessage("确定要删除 " + city.getName() + " 吗？")
                .setPositiveButton("删除", (dialog, which) -> {
                    cityAdapter.removeItem(position);
                    Toast.makeText(this, "已删除 " + city.getName(), Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("取消", null)
                .show();
    }
}